define(function(require,exports,module) {

    var myPub = require('./public');

    var httpImageUrl;
    var oHeader = $('.js_header');              	            //头部
    var oFooter = $('.js_footer');              	            //底部
    var oContain = $('.js_contain');				            //主体
    var oChangListBtn = $('.js_changListBtn');				    //改变产品展示按钮
    var oGoods_showChangeBox = $('.js_goods_showChangeBox');	//搜索展示的产品盒子
    var oChangePriceBtn = $('.js_changePrice'); 		        //价格按钮
    var oChangeSortDirBtn = $('.js_changeSortDir'); 		    //综合排序按钮
    var oSearchNav = $('.js_searchNav');				        //搜索列表导航
    var sortBy = "order_list";								    //排序


    var oMoreBtn = $('.js_moreBtn'); 			                //加载更多按钮

    var oloadingBtn = $(".js_loadingBtn");                      // 加载按鈕
    var noMoreBtn = $(".js_noMoreBtn");                         // 没有更多
    var oErrPage = $(".js_errPage");                            // 错误提示



    var lazLoading = new myPub.lazLoadingImg();
    oHeader.on("touchmove", function (e) {
        e.preventDefault()
    });
    oFooter.on("touchmove", function (e) {
        e.preventDefault()
    });
    //oSearchBox.on("touchmove",function(e) {e.preventDefault()});


    // 改变产品展示方式
    oChangListBtn.hammer().on('tap', function () {
        if ($(this).find('span').hasClass('icon-search_card')) {
            $(this).find('span').removeClass('icon-search_card').addClass('icon-search_list')
            oGoods_showChangeBox.removeClass('active');
            oGoods_showChangeBox.find('li figure').removeAttr('style')
        }
        else {
            $(this).find('span').addClass('icon-search_card').removeClass('icon-search_list')
            oGoods_showChangeBox.addClass('active');

            myPub.SetImgHeight(oGoods_showChangeBox);
        }

    });
    // 默认排序
    var sortDir = "desc";

    // 价格改变
    oChangePriceBtn.hammer().on('tap', function () {
        if ($(this).hasClass('on')) {
            //高到低
            $(this).removeClass('on');
            removeNode();
            sortDir = "asc";

        }
        else {
            //低到高
            $(this).addClass('on');
            removeNode();
            sortDir = "desc";
        }

    })
    // 改变综合排序
    oChangeSortDirBtn.hammer().on('tap', function () {
        if ($(this).hasClass('on')) {
            //高到低
            $(this).removeClass('on');
            removeNode();
            sortDir = "desc";

        }
        else {
            //低到高
            $(this).addClass('on');
            removeNode();
            sortDir = "asc";
        }

    })


    // 导航点击
    oSearchNav.find('li').hammer().on('tap', function () {
        var index = $(this).index();
        if (index == 0) {
            sortBy = "order_list";
        } else if (index == 1) {
            sortBy = "sales_count";
        } else if (index == 2) {
            sortBy = "sale_price";
            removeNode();
        }

        //第四个li是展示方式改变
        if (index !== 3) {
            oSearchNav.find('li').removeClass('active').eq(index).addClass('active');
            removeNode();
            start = 1;//切换导航初始化start
            loadData();
        }
    })

    oMoreBtn.hammer().on('tap', function () {
        var _this = $(this);
        _this.hide().siblings('.js_loadingBtn').show();

        //显示更多
        ++start;
        loadData();

        myPub.HamstopPropaga();
    })


    module.exports = {
        init:function(params1, params2) {
            httpImageUrl = params1;
            mshopdomain = params2;
            //产品懒加载  异步加载 调用这个
            lazLoading.AddLazLoadingImg();
            loadData();
        }
    }

    var keyword = $("#keyword").val();
    var page = 1;
    var start = 0;
    var limit = 20;
    var tmp = '';
    var imageUrl = "";

    //异步加载ajax请求  根据导航排序所有
    function loadData() {
        var url = "/shop/goods/searchv4/" + sortBy;
        var html = '';
        $.ajax({
            url: url,
            type: "post",
            data: {sortDir: sortDir, start: start, limit: limit, keyword: keyword},
            dataType: "json",
            success: function (data) {
                if (data != null) {
                    var goodsList = eval(data);
                    if (goodsList.length == 0) {
                        loadBtnOff('noMore');
                    }
                    else {
                        //$('#listAppend_'+(page)+'').remove();
                        var m = '';
                        var t = null;

                        $.each(goodsList, function (index, obj) {
                            t = viewList(obj);
                            m = m + t;
                        });
                        html = html + m;
                        $('#listAppend_' + (page - 1) + '').append(html);

                        // 判断是双排还是一排 双排需要设置图片大小
                        if( oGoods_showChangeBox.hasClass('active') ) {
                            myPub.SetImgHeight(oGoods_showChangeBox);
                        }

                        if (goodsList.length < limit) {
                            loadBtnOff('noMore');
                        } else {
                            loadBtnOff('More');
                        }

                    }
                } else {
                    loadBtnOff('error');
                }


            },
            complete: function (XHR) {
                lazLoading.DestroyLazLoadingImg();
                lazLoading.AddLazLoadingImg();
                XHR = null;

            }
        });
    }

    //切换导航时去除所有的html
    function removeNode() {
        $("#listAppend_0").find('li').remove();
    }

    // 数据拼接
    function viewList(obj) {
        //<#if isstatic=="true">${mshopdomain}/shop/goods/html/${obj.goodsSn}<#else>/h/goodspay/goodsView/${obj.goodsSn}</#if>
        //先拼接价格字符串，团购商品需同时显示团购价
        var priceStr = "";
        if (obj.isTuangou == true && typeof(obj.tuangouPrices) != "undefined") {
            priceStr =
                '<span class="goodsPrice">￥' + obj.tuangouPrices + '</span>' +
                '<span class="goodsWeight">/</span>' +
                '<span class="goodsWeight del Bflex1" style="margin-left: 0;">￥' + obj.salePrice + '</span>';
        } else {
            priceStr =
                '<span class="goodsPrice">￥' + obj.salePrice + '</span>' +
                '<span class="goodsWeight Bflex1">/' + obj.productName + '</span>';
        }
        var temp;
        if (typeof(obj.rfName) != "undefined") {

            var html_goodsLabels = '';
            if (obj.goodsLabels && obj.goodsLabels != '') {
                html_goodsLabels = '<span class="promotionHotIcon">' + obj.goodsLabel + '</span>'
            }

            var html_labels = '';
            if (obj.label && obj.label != '') {
                html_labels += '<div class="promotionSale horpromotionSale">';
                for (var i = 0; i < obj.labels.length; i++) {
                    html_labels += '<i>' + obj.labels[i] + '</i>'
                }
                html_labels += '</div>';
            }


            tmp =
                '<li>' +
                '   <a class="BoxSelf js_BoxSelf" href="/h/goodspay/goodsView/' + obj.goodsSn + '" onclick="GIOSearchDataClick(' + keyword + ',' + obj.goodsCategoryId + ',' + obj.id + ',' + obj.name + ',' + obj.goodsSn + ')"  requestid="'+ obj.requestid +'">' +
                '       <figure>'+
                '           <img class="js_lazyImg" data-echo="' + httpImageUrl + obj.showImage + smallImgArg300 + '"' + ' src="/static_v4/image/loadImg.jpg">'+
                '           <i class="tip-city">' + obj.rfName + '</i>' +
                '           <i class="tip-th" style="display:none"></i>' +
                '       </figure>' +
                '       <aside class="priceArea VBox Bflex1">' +
                '           <div class="Bflex1 priceArea_name">'+
                '               <h4>' + html_goodsLabels + obj.name + '</h4>' +
                '               <p class="smallFont_tip">' + getTitle(obj.title) + '</p>' +
                '           </div>' +
                '           <div class="priceArea_price">' +
                '               <p class="HBox BoxCenter priceAreaBox">' + priceStr  + '</p>' +
                '           </div>' +
                '       </aside>' +
                '   </a>' +
                '   <p class="addCar js_addCar" pId="'+ obj.productId +'" requestid="'+ obj.requestid +'"><em class="icon-shopcar_small"></em></p>' +
                html_labels +
                '</li>';
        } else {
            var html_goodsLabels = '';
            if (obj.goodsLabels && obj.goodsLabels != '') {
                html_goodsLabels = '<span class="promotionHotIcon">' + obj.goodsLabel + '</span>'
            }

            var html_labels = '';
            if (obj.label && obj.label != '') {
                html_labels += '<div class="promotionSale horpromotionSale">';
                for (var i = 0; i < obj.labels.length; i++) {
                    html_labels += '<i>' + obj.labels[i] + '</i>'
                }
                html_labels += '</div>';
            }


            tmp =
                '<li>' +
                '   <a class="BoxSelf js_BoxSelf" href="/h/goodspay/goodsView/' + obj.goodsSn + '" onclick="GIOSearchDataClick(' + keyword + ',' + obj.goodsCategoryId + ',' + obj.id + ',' + obj.name + ',' + obj.goodsSn + ')" requestid="'+ obj.requestid +'">' +
                '       <figure>' +
                '           <img class="js_lazyImg" data-echo="' + httpImageUrl + obj.showImage + smallImgArg300 + '"' + ' src="/static_v4/image/loadImg.jpg">' +
                '           <i class="tip-th" style="display:none"></i>' +
                '       </figure>' +
                '       <aside class="priceArea VBox Bflex1">' +
                '           <div class="Bflex1 priceArea_name">' +
                '               <h4>' + html_goodsLabels +  obj.name + '</h4>' +
                '               <p class="smallFont_tip">' + getTitle(obj.title) + '</p>' +
                '           </div>' +
                '           <div class="priceArea_price">' +
                '               <p class="HBox BoxCenter priceAreaBox">' + priceStr + '</p>' +
                '           </div>' +
                '       </aside>' +
                '   </a> ' +
                '   <p class="addCar js_addCar" pId="' + obj.productId + '" requestid="'+ obj.requestid +'"><em class="icon-shopcar_small"></em></p>' +
                html_labels +
                '</li>';
        }

        return tmp;
    }

    // 标题设置
    function getTitle(obj) {
        if (obj == null) {
            return null;
        } else if (obj == 'undefined') {
            return null;
        } else {
            return obj;
        }

    }


    // 显示按钮
    function loadBtnOff (type) {
        switch (type) {
            case 'noMore':
                oloadingBtn.hide();
                noMoreBtn.show();
                oMoreBtn.hide();
                oErrPage.hide();
                break;
            case 'More':
                oloadingBtn.hide();
                noMoreBtn.hide();
                oMoreBtn.show();
                oErrPage.hide();
                break;
            case 'error':
                oloadingBtn.hide();
                noMoreBtn.hide();
                oMoreBtn.hide();
                oErrPage.show();
                break;
            default:
                break;
        }
    }
});